import time, random
from multiprocessing import Process, Pipe, current_process
from multiprocessing.connection import wait

def foo(w):
    for i in range(10):
        msg = f"{current_process().pid} {current_process().name} {i}: hello"
        w.send(msg)
        time.sleep(1)
    w.close()

if __name__ == "__main__":
    reader = []
    for i in range(4):
        r, w = Pipe(duplex=False)
        reader.append(r)
        p = Process(target=foo, args=(w,))
        p.start()
        w.close()

    while reader:
        for r in wait(reader):
            try:
                msg = r.recv()
            except EOFError:
                reader.remove(r)
            else:
                print("接收到消息：", msg)
